<template>
  <div class="wrapper" style="margin-bottom: -49px">
    <h2 class="logo">
      <img :src="logo.pic" alt="" />
    </h2>
    <van-form @submit="onSubmit">
      <van-field
        v-model="register.phone"
        autocomplete="off"
        name="phone"
        label="手机号"
        placeholder="手机号"
        :rules="[
          { required: true, message: '手机号码不能为空' },
          { pattern: phonePattern, message: '手机号的格式有误！请重新输入' },
        ]"
      />
      <van-field
        v-model="register.username"
        autocomplete="off"
        type="text"
        name="username"
        label="用户名"
        placeholder="用户名"
        :rules="[
          { required: true, message: '用户名不能为空' },
          { pattern: userPattern, message: '用户名的格式有误！请重新输入' },
        ]"
      />
      <van-field
        v-model="register.password"
        autocomplete="off"
        :type="type"
        name="password"
        :right-icon="type == 'password' ? 'closed-eye' : 'eye-o'"
        @click-right-icon="changeType"
        label="密码"
        placeholder="密码由6-16位数字加字母组成"
        :rules="[
          { required: true, message: '密码不能为空' },
          { pattern: $reg.pwd, message: '密码的格式有误！请重新输入' },
        ]"
      />
      <van-field
        v-model="register.dbpass"
        :type="type1"
        name="dbpass"
        label="确认密码"
        @click-right-icon="changeType1"
        :right-icon="type1 == 'password' ? 'closed-eye' : 'eye-o'"
        placeholder="6-16位数字加字母"
        :rules="[
          { required: true, message: '请填写确认密码' },
          { validator: checkPassEqual, message: '两次密码输入不一致' },
        ]"
      />
      <van-field name="agree" :rules="[{ required: true, message: '请选择' }]">
        <template #input>
          <van-checkbox v-model="register.agree">请同意我们的条款</van-checkbox>
        </template>
      </van-field>
      <div style="margin: 16px">
        <van-button round block type="info" native-type="submit"
          >提交</van-button
        >
      </div>
    </van-form>
  </div>
</template>

<script>
import obj from '@/utils/mixin'

export default {
  name: 'register',
  mixins: [obj],
  data () {
    return {
      logo: { pic: require('@/assets/logo.png') },
      register: {},
      phonePattern: this.$reg.phone,
      userPattern: this.$reg.account,
      type: 'password',
      type1: 'password'
    }
  },
  methods: {
    onSubmit (value) {
      console.log(value)
      this.$ajax.register(value).then((res) => {
        if (res.code == 200) {
          this.$router.push({
            name: 'login',
            query: { username: value.username }
          })
        }
      })
    },
    checkPassEqual (val) {
      return this.register.password == val
    },
    changeType () {
      this.type = this.type == 'password' ? 'text' : 'password'
    },
    changeType1 () {
      this.type1 = this.type1 == 'password' ? 'text' : 'password'
    }
  }
}
</script>

<style lang="scss" scoped>
.wrapper {
  background-color: #fff;
  .logo {
    overflow: hidden;
    text-align: center;
    img {
      margin: 79px auto 40px;
      height: 60px;
      line-height: 60px;
    }
  }
}
.van-button--info {
  margin-top: 40px;
  background-color: #ff5f16;
  border-color: #ff5f16;
}
</style>
